
import 'package:flutter/material.dart';
import 'package:keep_account_web/utils/screen_utils.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

import '../bean/work_record.dart';
import '../utils/datetime_utils.dart';

class MonthlySalaryLineChart extends StatelessWidget{

  final DeviceScreenType screenType;
  final List<WorkRecord> workRecords;
  const MonthlySalaryLineChart({super.key,required this.screenType,required this.workRecords});

  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
      primaryXAxis: CategoryAxis(
          isVisible: true
      ),
      onChartTouchInteractionUp: null,
      selectionType: SelectionType.series,
      trackballBehavior: TrackballBehavior(
          lineType: TrackballLineType.vertical,
          enable: true,
          tooltipSettings: const InteractiveTooltip(
              color: Colors.purple,
              connectorLineColor: Colors.yellow
          ),
          tooltipAlignment: ChartAlignment.center,
          shouldAlwaysShow: true,
          tooltipDisplayMode: TrackballDisplayMode.floatAllPoints
      ),
      tooltipBehavior: TooltipBehavior(
          enable: true,
          shared: true,
          borderColor: Theme.of(context).colorScheme.onBackground,
          activationMode: ActivationMode.singleTap,
          builder: (data, point, series, pointIndex, seriesIndex){
            return toolTitle(context,data);
          }
      ),
      series: [
        ColumnSeries(
            name: "${getCurrentYear()}年月工资",
            width: .1,
            color: Theme.of(context).colorScheme.secondary,
            dataSource: workRecords,
            xValueMapper: (en,_) => en.workDate?.substring(5) ?? "-",
            yValueMapper: (en,_) => en.totalSalary,
            enableTooltip: true,
            dataLabelSettings: DataLabelSettings(
              isVisible: true,
              textStyle: TextStyle(fontSize: 15,color: Theme.of(context).colorScheme.secondary),
              labelAlignment: ChartDataLabelAlignment.outer,
            )
        )
      ],
    );
  }

  Widget toolTitle(BuildContext context,WorkRecord workRecords){
    return Container(
      width: 120,
      height: 100,
      padding: const EdgeInsets.all(5),
      decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.onBackground
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(workRecords.workDate ?? "",style:  const TextStyle(fontSize: 14),),
          Text("工资：${workRecords.totalSalary}",style:  const TextStyle(fontSize: 14)),
          Text("数量：${(workRecords.teamQuantity ?? .0) + (workRecords.singleQuantity ?? .0)}",style:  const TextStyle(fontSize: 14)),
          Visibility(
              visible: (workRecords.singleQuantity ?? 0) > 0,
              maintainAnimation: false,
              maintainSize: false,
              maintainState: false,
              child: Text("个人件数: ${workRecords.singleQuantity}",style: const TextStyle(fontSize: 12),)
          )
        ],
      ),
    );
  }

}